package com.zhangkunhao.notes.web.filter;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.PermissionBlacklist;
import com.zhangkunhao.notes.entity.Do.User;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.service.PermissionBlacklistServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户不同权限的拦截器
 *
 * @author 20642
 */
@WebFilter(value = {"/question/thumpsUp", "/question/askQuestion", "/answer/answerQuestion", "/answerComment/publishComment"})
public class PermissionsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 强转获取HttpServletRequest对象
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        // 获取Session对象
        HttpSession session = httpRequest.getSession();

        // 获取Session中保存的数据,可能是null
        Integer userId = (Integer) session.getAttribute("userId");
        System.out.println("权限拦截器userId: " + userId);

        //获取当前访问url
        String url = httpRequest.getRequestURL().toString();

        //查询
        PermissionBlacklistServiceImpl blacklistService = new PermissionBlacklistServiceImpl();
        List<PermissionBlacklist> permissionBlacklistList = blacklistService.selectAll().stream()
                .filter(permissionBlacklist -> permissionBlacklist.getBanUserId().equals(userId)).collect(Collectors.toList());
        //判断校验
        if (!permissionBlacklistList.isEmpty()) {
            if (url.contains("/question/thumpsUp")) {
                for (PermissionBlacklist permissionBlacklist : permissionBlacklistList) {
                    if (permissionBlacklist.getBanPermissionName().equals("点赞")) {
                        //判断是否封禁时间已经到了
                        if (permissionBlacklist.getOpenDate().compareTo(new Timestamp(System.currentTimeMillis())) >= 0) {
                            //返回数据
                            Message<User> message = new Message<>(null, "你的该权限已经被管理员封禁了，无法操作", 0);
                            response.setContentType("text/json;charset=UTF-8");
                            response.getWriter().write(JSON.toJSONString(message));
                            System.out.println("你的该权限已经被管理员封禁了，无法操作");
                            return;
                        } else {
                            //放行
                            System.out.println("点赞放行");
                            chain.doFilter(httpRequest, response);
                            return;
                        }
                    }
                }
            }
            if (url.contains("/answer/answerQuestion")) {
                for (PermissionBlacklist permissionBlacklist : permissionBlacklistList) {
                    if (permissionBlacklist.getBanPermissionName().equals("回答")) {
                        //判断是否封禁时间已经到了
                        if (permissionBlacklist.getOpenDate().compareTo(new Timestamp(System.currentTimeMillis())) >= 0) {
                            Message<User> message = new Message<>(null, "你该权限已经被管理员封禁了，无法操作", 0);
                            response.setContentType("text/json;charset=UTF-8");
                            response.getWriter().write(JSON.toJSONString(message));
                            System.out.println("你的该权限已经被管理员封禁了，无法操作");
                            return;
                        } else {
                            //放行
                            System.out.println("回答问题放行");
                            chain.doFilter(httpRequest, response);
                            return;
                        }
                    }
                }
            }
            if (url.contains("/question/askQuestion")) {
                for (PermissionBlacklist permissionBlacklist : permissionBlacklistList) {
                    System.out.println("封禁的权限名称：" + permissionBlacklist.getBanPermissionName());
                    if (permissionBlacklist.getBanPermissionName().equals("发布问题")) {
                        //判断是否封禁时间已经到了
                        if (permissionBlacklist.getOpenDate().compareTo(new Timestamp(System.currentTimeMillis())) >= 0) {
                            Message<User> message = new Message<>(null, "你该权限已经被管理员封禁了，无法操作", 0);
                            response.setContentType("text/json;charset=UTF-8");
                            response.getWriter().write(JSON.toJSONString(message));
                            System.out.println("你的该权限已经被管理员封禁了，无法操作");
                            return;
                        } else {
                            //放行
                            System.out.println("问问题放行");
                            chain.doFilter(httpRequest, response);
                            return;
                        }
                    }
                }
            }
            if (url.contains("/answerComment/publishComment")) {
                for (PermissionBlacklist permissionBlacklist : permissionBlacklistList) {
                    if (permissionBlacklist.getBanPermissionName().equals("评论")) {
                        //判断是否封禁时间已经到了
                        if (permissionBlacklist.getOpenDate().compareTo(new Timestamp(System.currentTimeMillis())) >= 0) {

                            Message<User> message = new Message<>(null, "你该权限已经被管理员封禁了，无法操作", 0);
                            response.setContentType("text/json;charset=UTF-8");
                            response.getWriter().write(JSON.toJSONString(message));
                            System.out.println("你的该权限已经被管理员封禁了，无法操作");
                            return;
                        } else {
                            //放行
                            System.out.println("发评论放行");
                            chain.doFilter(httpRequest, response);
                            return;
                        }
                    }
                }
            }
        }
        //正常用户，放行
        chain.doFilter(httpRequest, response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }


}
